package com.example.littleken.listadapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    protected Button btn_go;
    protected ListView lv;
    List<Map<String,Object>> list;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);//初始化控件
        btn_go = (Button) findViewById(R.id.btn_go);
        btn_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });

        //数据准备
        list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("logo",R.drawable.find_main_hotwind);
        map.put("title","千千静听");
        map.put("version","版本：1.0");
        map.put("size","19M");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("logo",R.drawable.find_main_square);
        map.put("title","时空猎人");
        map.put("version","版本：2.0");
        map.put("size","23M");
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("logo",R.drawable.find_main_travel);
        map.put("title","coc");
        map.put("version","版本：2.1");
        map.put("size","500M");
        list.add(map);


        //设置适配器
        SimpleAdapter adapter = new SimpleAdapter(
                this,//上下文
                list,//数据源
                R.layout.listview_layout,//使用布局xml
                new String[]{"logo","title","version","size"},//对应关系
                new int[]{R.id.logo,R.id.title,R.id.version,R.id.size}
        );
        lv.setAdapter(adapter);//关联控件与适配器

        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map map = list.get(position);//为了获取资源id
        Toast.makeText(this,""+map.get("title"),Toast.LENGTH_SHORT).show();
    }
}
